#!/bin/bash
# 
# To successfully employ this script, make sure that your bash is in
# the bin - directory or adjust accordingly.
#

DIRS=`find Process/Amegic/ -name P?_?`" "`find Process/Amegic/ -name P?_??`

print_help(){
  echo "makelibs version 1.1" && echo && \
  echo "options: -c \"make clean\" before compiling" && \
  echo "         -n skip \"make distclean\" after compiling" && \
  echo "         -o install single module" && \
  echo "         -j [N] allow N compilations at once" && \
  echo "         -m create one library per process" && \
  echo "         -s force use of make" && \
  echo "         -i set include path" && \
  echo "         -h display this help and exit" && echo
}

defaults(){
  JOBS=2
  LIBMODE=2
}

defaults;

which scons > /dev/null 2>&1 && SCONS=TRUE
while getopts hsmcnoj:i: OPT
do
  case $OPT in
  c) CLEAN=TRUE ;;
  n) POSTCLEAN=FALSE ;;
  o) ONE=TRUE ;;
  j) JOBS=$OPTARG ;;
  m) LIBMODE=1 ;;
  s) SCONS=FALSE ;;
  i) IPATH=$OPTARG ;;
  h) print_help && exit 0 ;;
  \?)
    shift `expr $OPTIND - 1`
    if [ "$1" = "--help" ]; then print_help && exit 0;
    else 
      echo -n "makelibs: error: unrecognized option "
      if [ $OPTARG != "-" ]; then echo "'-$OPTARG'. try '-h'"
      else echo "'$1'. try '-h'"
      fi
      print_help && exit 1
    fi
    shift 1
    OPTIND=1
  esac
done

if [ "$ONE" = "TRUE" ] ; then 
  echo "makelibs: select module ( q) to quit )"
  select DIRS in $DIRS ; do
    if [ "$DIRS" != "" ] ; then 
      SINGLE="TRUE"
      break
    else 
      if [ "$REPLY" = "q" ] || [ "$REPLY" = "Q" ]  ; then exit ; fi
    fi
  done
fi

if [ "$SCONS" = "TRUE" ] ; then
  test -z "$IPATH" || IPATH="incdir="$IPATH
  @BINDIR@/make2scons x;
  for J in $DIRS ; do
    @BINDIR@/make2scons $J $LIBMODE;
  done
  scons -j$JOBS install $IPATH;
  exit 0;
fi

if ! test -z "@CXX@"; then export CXX="@CXX@"; fi
export CXXFLAGS="@CXXFLAGS@ -O2";
if ! test -z "@LDFLAGS@"; then export LDFLAGS="@LDFLAGS@"; fi
if ! test -z "@LIBS@"; then export LIBS="@LIBS@"; fi
if ! test -z "@CPPFLAGS@"; then export CPPFLAGS="@CPPFLAGS@"; fi
if ! test -z "@CC@"; then export CC="@CC@"; fi
if ! test -z "@CFLAGS@"; then export CFLAGS="@CFLAGS@"; fi
if ! test -z "@FC@"; then export FC="@FC@"; fi
if ! test -z "@FCFLAGS@"; then export FCFLAGS="@FCFLAGS@"; fi
if ! test -z "@CPP@"; then export CPP="@CPP@"; fi
if ! test -z "@CXXCPP@"; then export CXXCPP="@CXXCPP@"; fi

if echo "@host@" | grep -q "i686"; then
    export CXX="$CXX -m32"
    export CC="$CC -m32"
    export LDFLAGS="$LDFLAGS -m32"
fi

test -z "$IPATH" || IPATH="CURRENT_SHERPASYS="$IPATH
for J in $DIRS ; do
  echo "======================"
  echo "$J";
  echo "======================"
  cd $J 
  test -f Makefile.am.orig || cp Makefile.am Makefile.am.orig
  test -f configure.in.orig || cp configure.in configure.in.orig
  if test $LIBMODE -eq 1; then
    cp configure.in.orig configure.in
    diff -q Makefile.am Makefile.am.orig || cp Makefile.am.orig Makefile.am
  elif test $LIBMODE -eq 2; then
    sed -r '/[1-2]_[0-9*]__/ d' < configure.in.orig > configure.in
    libname="Proc_"$(basename $(pwd))
    sources=$(for i in $(find . -name \*.C | grep -v fsrchannels); do printf $i | sed 's/$/ /1' | tr '\n' ' '; done)
    echo "AUTOMAKE_OPTIONS = subdir-objects
lib_LTLIBRARIES = lib${libname}.la
lib${libname}_la_SOURCES = ${sources}
CURRENT_SHERPASYS ?= @INCLUDEDIR@
INCLUDES = -I\$(CURRENT_SHERPASYS)" > Makefile.am.new
    fsrdir=$(find . -name fsrchannels*)
    test -d $fsrdir && echo "SUBDIRS = ${fsrdir}" >> Makefile.am.new
    diff -q Makefile.am Makefile.am.new || mv Makefile.am.new Makefile.am
  fi
  if test -f configure.in; then
  autoreconf -fi
  ./configure --libdir='${prefix}/lib'
  if `test "$CLEAN" = "TRUE"` ; then make clean ; fi
  if ! make install -j $JOBS $IPATH; then
    echo "makeinstall: error: \"make install\" in $I failed"
    exit 1
  fi
  if `! test "$POSTCLEAN" = "FALSE"` ; then make distclean ; fi
  fi
  cd -
done
